﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Configuration;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace netsoul
{
    public partial class FrmChat : Form
    {
        #region Fields
        private Netsoul Fnet;
        private string Flogin;
        private string Flocation;
        private dlgUpdateChat dlgUpChat = null;
        private string Ftxtmsg = string.Empty;
        private Bitmap FAvatar;


        #endregion Fields

        public FrmChat()
        {
            InitializeComponent();
        }

        public FrmChat(string login)
        {
            InitializeComponent();
            this.Flogin = login.Trim();
            this.txt_login.Text = Flogin.Trim();
            this.imgbox_avatar.ImageLocation = "http://www.epitech.net/intra/photo.php?login=" + Flogin;
            FAvatar = GetAvatar(Flogin);
            this.imgbox_avatar.Image = FAvatar;
        }

       public FrmChat(string login, Netsoul n)
        {
            InitializeComponent();
            this.Flogin = login.Trim();
            this.txt_login.Text = Flogin.Trim();
            this.imgbox_avatar.ImageLocation = "http://www.epitech.net/intra/photo.php?login=" + Flogin;
            FAvatar = GetAvatar(Flogin);
            this.imgbox_avatar.Image = FAvatar;
        }

        public FrmChat(string login, string location, Netsoul n)
        {
            InitializeComponent();
           this.Flocation = location.Trim();
            this.Flogin = login.Trim();
            this.imgbox_avatar.ImageLocation = "http://www.epitech.net/intra/photo.php?login=" + Flogin;
            FAvatar = GetAvatar(Flogin);
            this.imgbox_avatar.Image = FAvatar;
            this.Fnet = n;
        }

        #region Methodes

        #region Event

        private void btn_sendmsg_Click(object sender, EventArgs e)
        {
            int nb = 1;
            string msg;

            if (txt_msgedit.Text.Trim().Length <= 0)
                return;
            msg = txt_msgedit.Text.Trim();

            if (Convert.ToInt32(txt_nb_spam.Text) > 1 && Convert.ToInt32(txt_nb_spam.Text) < 10000)
                nb = Convert.ToInt32(txt_nb_spam.Text);

            
            if (Flocation.Trim().Length > 0)
            {
                while (nb > 0)
                {
                    if (nb > 1)
                        msg = "[" + nb.ToString() + "]" + txt_msgedit.Text.Trim();
                    Fnet.Send_message_login(Flogin, msg.Trim(), Flocation);
                    nb--;
                }
            }
            else
            {
                while (nb > 0)
                {
                    if (nb > 1)
                        msg = "[" + nb.ToString() + "]" + txt_msgedit.Text.Trim();
                    Fnet.Send_message_login(Flogin, msg.Trim());
                    nb--;
                }
            }
            txt_chat.Text += "Me " + DateTime.Now.ToString("hh:mm:ss") + " > " + txt_msgedit.Text.Trim() + "\r\n";
            txt_msgedit.Text = string.Empty;
        }

        #endregion Event

        private static Bitmap GetAvatar(string login)
        {

            HttpWebRequest wreq;
            HttpWebResponse wresp = null;
            Stream stream = null;
            Bitmap bmp = null;
            Bitmap avatar = null;

            try
            {
                wreq = (HttpWebRequest)WebRequest.Create("http://www.epitech.net/intra/photo.php?login=" + login.Trim());
                wreq.AllowWriteStreamBuffering = true;

                throw new Exception("?");

                WebProxy proxy = WebProxy.GetDefaultProxy();
                wreq.Proxy = proxy;


                wresp = (HttpWebResponse)wreq.GetResponse();
                if ((stream = wresp.GetResponseStream()) != null)
                    bmp = new Bitmap(stream);
                avatar = new Bitmap(100, 120);
                using (Graphics g = Graphics.FromImage((Image)avatar))
                    g.DrawImage(bmp, 0, 0, 100, 120);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (stream != null)
                    stream.Close();
                if (wresp != null)
                    wresp.Close();
                if (bmp != null)
                    bmp.Dispose();  
            }
            return avatar;
        }

        public delegate void dlgUpdateChat(string txt);

        public void UpdateChat(string txt)
        {
            if (dlgUpChat == null)
                dlgUpChat = new dlgUpdateChat(UpdateChat);
            if (this.InvokeRequired)
                this.Invoke(dlgUpChat, txt);
            else
            {
                txt_login.Text = Flogin.Trim();
                string rep = Netsoul.DecodeMSG(txt);
                txt_chat.Text += Flogin.Trim() + " " + DateTime.Now.ToString("hh:mm:ss") + " > " + Netsoul.DecodeMSG(txt) + "\r\n";
            }
        }

        [System.Runtime.InteropServices.DllImport( "user32.dll" )]
        private static extern bool FlashWindow ( IntPtr hwnd, bool bInvert );


        #endregion Methodes

        #region Proprietes

        public string TxtLogin
        {
            get
            {
                return txt_login.Text.Trim();
            }
            set
            {
                txt_login.Text = value;
            }
        }

        public string TxtChat
        {
            get
            {
                return txt_chat.Text.Trim();
            }
            set
            {
                UpdateChat(value);
            }
        }

        #endregion Proprietes
    }
}
